var express = require("express");
var router = express.Router();
const path = require("path");
// import formidable from "formidable";
const formidable = require("formidable");

router.get("/portrait", function (req, res, next) {
  res.render("portrait", { title: "文件上传" });
});

// 上传文件
router.post("/portrait", (req, res, next) => {
  const form = formidable({
    multiples: true,
    uploadDir: path.resolve(__dirname, "../public/other"), // 静态文件上传地址
    keepExtensions: true, // 保存后缀名
  });

  form.parse(req, (err, fields, files) => {
    if (err) {
      next(err);
      return;
    }
    // res.json({ fields, files });
    console.log(fields);
    console.log(files.file.newFilename);
    const path = "/other/" + files.file.newFilename;
    res.send(path);
  });
});

module.exports = router;
